package com.aier.service.forwarder.impl;


import com.aier.bean.forwarder.FreightForwarder;
import com.aier.mapper.forwarder.FreightForwarderMapper;
import com.aier.service.forwarder.FreightForwarderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 货代service实现类
 */
@Service
public class FreightForwarderServiceImpl implements FreightForwarderService {

    @Autowired
    private FreightForwarderMapper freightForwarderMapper;

    @Override
    public List<FreightForwarder> getFreightForwarderOneMonth() {
        LocalDate currentDate = LocalDate.now();
        LocalDate oneMonthAgo = currentDate.minus(1, ChronoUnit.MONTHS);
        QueryWrapper<FreightForwarder>queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(FreightForwarder::getQuoteDate,currentDate,oneMonthAgo);
        return freightForwarderMapper.selectList(queryWrapper);
    }

    @Override
    public FreightForwarder getFreightForwarderByParam(String warehouseCode, String forwarderCode, String sku, LocalDate quoteDate) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(FreightForwarder::getWarehouseCode,warehouseCode)
                .eq(FreightForwarder::getForwarderCode,forwarderCode)
                .eq(FreightForwarder::getSku,sku)
                .eq(FreightForwarder::getQuoteDate,quoteDate)
                .orderByAsc(FreightForwarder::getQuoteDate);
        return freightForwarderMapper.selectOne(queryWrapper);
    }

    @Override
    public FreightForwarder getFreightForwarderByParam(String warehouseCode, String forwarderCode, String sku) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(FreightForwarder::getWarehouseCode,warehouseCode)
                .eq(FreightForwarder::getForwarderCode,forwarderCode)
                .eq(FreightForwarder::getSku,sku)
                .orderByAsc(FreightForwarder::getQuoteDate);
        return freightForwarderMapper.selectOne(queryWrapper);
    }

    @Override
    public FreightForwarder getFreightForwarderByParam(String warehouseCode, String forwarderCode) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(FreightForwarder::getWarehouseCode,warehouseCode)
                .eq(FreightForwarder::getForwarderCode,forwarderCode)
                .orderByAsc(FreightForwarder::getQuoteDate);
        return freightForwarderMapper.selectOne(queryWrapper);
    }


    @Override
    public void saveFreightForwarder(FreightForwarder freightForwarder) {
        freightForwarderMapper.insert(freightForwarder);
    }

    @Override
    public void modifyFreightForwarder(FreightForwarder freightForwarder) {
        UpdateWrapper<FreightForwarder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(FreightForwarder::getForwarderId,freightForwarder.getForwarderId());
        freightForwarderMapper.update(freightForwarder,updateWrapper);
    }

    @Override
    public void removeFreightForwarder(String forwardId) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FreightForwarder::getForwarderId, forwardId); // 数据库字段名
        freightForwarderMapper.delete(queryWrapper);
    }

    @Override
    public List<FreightForwarder> getFreightForwarderByFba(String fbaCode) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FreightForwarder::getFbaCode, fbaCode);
        return freightForwarderMapper.selectList(queryWrapper);
    }

    @Override
    public FreightForwarder getFreightForwarderByFbaAndForwarderCode(String fbaCode, String forwarderCode) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(FreightForwarder::getFbaCode, fbaCode)
                .eq(FreightForwarder::getForwarderCode,forwarderCode);
        return freightForwarderMapper.selectOne(queryWrapper);
    }
}
